(function () {

    var label = document.getElementsByClassName('input-label');
    var location = document.getElementById('location');
    var inputBtn = document.getElementById('input-btn');

    var editor;

    initEditor();
    getLocation();

    inputBtn.addEventListener('click', inputCard);

    //初始化编辑器
    function initEditor() {
        var E = window.wangEditor;
        editor = new E(document.getElementById('editor-content'));
        editor.customConfig.zIndex = 100;
        editor.create();
    }

    // 获取定位信息
    function getLocation() {
        Api.ajax({
            method: 'GET',
            url: 'http://restapi.amap.com/v3/ip',
            data: {
                key: '405d4f074880ea8c53bd5f5022890841',
                output: 'JSON'
            },
            success: function (response) {
                console.log(response);
                location.innerHTML = response.city;
            }
        })
    }

    function getlabel() {
        var labelStr = '';
        for (var i = 0; i < label.length; i++) {
            if (label.item(i).checked == true) {
                labelStr += (i + ';');
            }
        }
        console.log(labelStr);
        return labelStr;
    }

    function inputCard() {
        console.log('inputCard');
        var userInfo = CommonUtil.isLogin();
        if (userInfo == null) {
            mdui.alert('尚未登录', function () {
                //TODO 跳转登录界面
                window.location.href = 'login.html';
            });
        }
        var locationStr = location.innerHTML+'';

        Api.ajax({
            method: 'POST',
            url: '/addCardForWeb',
            data: {
                content: editor.txt.html(),
                userId: userInfo.id,
                time: Date.parse(new Date()),
                label: getlabel(),
                site: locationStr.substring(0, locationStr.length - 1)
            },
            success: function (response) {
                console.log(response);
                if (response.code == 0) {
                    mdui.alert('发帖成功', function () {
                        //TODO 跳转帖子界面
                    });
                } else {
                    mdui.alert('发帖失败', function () {
                        //TODO 跳转帖子界面
                    });
                }
            }
        })
    }


})();